package itsm.isperp.framework.upload.impl;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.domain.UploadInfo;
import itsm.isperp.framework.upload.TempFilePersister;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * 默认的临时文件存储实现类，默认实现类是将文件存储到当前系统的临时目录/temp目录下
 */
public class DefaultTempFilePersister implements TempFilePersister {

	@Override
	public InputStream get(String id) throws Exception {
		String path = ContextHolder.getTempFileStorePath() + id;
		return new FileInputStream(path);
	}

	public String persist(InputStream in, UploadInfo uploadInfo)
			throws Exception {
		return uploadInfo.getName();
	}

	public void delete(String id) throws Exception {
		String path = ContextHolder.getTempFileStorePath() + id;
		File f = new File(path);
		if (f.exists()) {
			f.delete();
		}
	}
}
